<template>
  <el-dialog title="设置采购核销状态" :visible.sync="dialogVisible" width="800px" :close-on-click-modal="false" append-to-body
    :modal-append-to-body="false" @close="dialogVisible=false" v-drag2anywhere>
    <el-form :model="formData" size="mini" label-width="120px">
      <el-row>
        <el-col :span="8">
          <el-form-item label="进项票号:">
            {{formData.num}}
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="进项总额:">
            {{parseFloat(formData.sum).toFixed(4)}}
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="采购已核销金额:">
            {{parseFloat(formData.buy_bindsum).toFixed(4)}}
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="采购核销额:">
            <span style="font-weight: 700; ">{{(parseFloat(formData.sum)-parseFloat(formData.buy_bindsum)).toFixed(4)}}</span>
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="采购核销状态:">
            <el-select v-model="buy_status" placeholder="请选择核销状态" style="width: 120px;">
              <el-option label="未核销" value="0"></el-option>
              <el-option label="已核销" value="1"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="核销备注:" label-width="120px">
            <el-input type="textarea" v-model="formData.buy_statusmemo" placeholder="核销状态备注信息" :rows="3"></el-input>
          </el-form-item>
        </el-col>

      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false" size="medium">取 消</el-button>
      <el-button type="primary" size="medium" @click="handleSave">保存</el-button>
    </div>
  </el-dialog>
</template>

<script>
  export default {
    data() {
      return {
        index: -1,
        formData: {
          num: '',
          sum: 0,
          buy_bindsum: 0,
          buy_status: 0,
          buy_statusmemo: ''
        },
        buy_status: "0",
        dialogVisible: false
      }
    },
    methods: {
      initData(index, data) {
        this.index = index;
        this.formData = data;
        this.buy_status = data.buy_status;
        console.log(data);
      },
      handleSave() {
        if (this.buy_status == 1 && (parseFloat(this.formData.sum) - parseFloat(this.formData.buy_bindsum)) > 0 && this
          .formData.buy_statusmemo == "") {
          this.$message.error({
            message: "此项存在未核销差额,请备注核销原因！",
            duration: 1500,
            showClose: true
          });
          return;
        }
        const params = {
          id: this.formData.id,
          status: this.buy_status,
          memo: this.formData.buy_statusmemo,
          num: this.formData.num
        }
        this.axios.post("api/admin/buyticketdetail/forcebuystatus", params).then(res => {
          if (res.data.code == 1) {
            this.$message.success({
              message: res.data.msg,
              duration: 1500,
              showClosed: true
            });
            this.$emit("updated", this.index, params);
            this.dialogVisible=false;
          } else {
            this.$message.error({
              message: res.data.msg,
              duration: 1500,
              showClosed: true
            });
          }
        })
      }
    }
  }
</script>

<style>
</style>
